package com.shixianjun.sort;

public class SortCase {

    public long startTime;
    public long endTime;
    public long duration;

    // 原始数据
    public SortableElement[] original;
    // 自定义排序器 排序结果
    public SortableElement[] customResult;
    // 系统方法    排序结果
    public SortableElement[] systemResult;

    // 结果是否相同
    public boolean same;
    /* 结果是否稳定 */
    public boolean stable;

    public boolean same() {
        if (customResult.length != systemResult.length) return false;

        for (int i = 0; i < customResult.length; i++) {
            if (customResult[i].value != systemResult[i].value) {
                return false;
            }
        }
        return true;
    }

    public boolean stable() {
        if (customResult.length != systemResult.length) return false;

        for (int i = 0; i < customResult.length - 1; i++) {
            if (customResult[i].value == customResult[i + 1].value && customResult[i].id > customResult[i + 1].id) {
                return false;
            }
        }
        return true;
    }

    public void print() {
        printValue("原始数组空空空空 value", original);
        printValue("自定义排序后数组 value", customResult);
        printValue("系统排序后数组空 value", systemResult);

        System.out.print(same ? "排序结果通过\n" : "排序结果未通过\n");

        printId("原始数组空空空空 id", original);
        printId("自定义排序后数组 id", customResult);
        printId("系统排序后数组空 id", systemResult);

        System.out.print(stable ? "排序结果稳定\n" : "排序结果不稳定\n");
        System.out.print("耗时(ms)：" + this.duration + "\n");
    }

    void printValue(String title, SortableElement[] arr) {
        System.out.print(title + ": ");
        for (int i = 0; i < arr.length; i++) {
            System.out.printf(" %02d", arr[i].value);
        }
        System.out.print("\n");
    }

    void printId(String title, SortableElement[] arr) {
        System.out.print(title + ": ");
        for (int i = 0; i < arr.length; i++) {
            System.out.printf(" %02d", arr[i].id);
        }
        System.out.print("\n");
    }

}
